package com.atguigu.srb.core.controller.api;


import com.atguigu.guigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.LendReturn;
import com.atguigu.srb.core.service.LendReturnService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/api/core/lendReturn")
public class ApiLendReturnController {
    @Autowired
    LendReturnService lendReturnService;
    ///api/core/lendReturn/notifyUrl
    @ApiOperation("还款回调")
    @PostMapping("/notifyUrl")
    public String repaymentNotify(HttpServletRequest request){
        String result = lendReturnService.repaymentNotify(request);
        return result;
    }
    @ApiOperation("还款")
    @PutMapping("/auth/repayment/{id}")
    public R repayment(@PathVariable("id")String id,
                       @RequestHeader(value = "token",required = false)
                       String token){
        //构建访问hfb还款接口的自动提交的表单字符串
        String formStr = lendReturnService.repayment(id,token);
        return R.ok().data("formStr",formStr);
    }

    @ApiOperation("获取还款计划")
    @GetMapping("/lend/{lendId}")
    public R list(@PathVariable("lendId")String lendId){
        List<LendReturn> lendReturns = lendReturnService.list(Wrappers.lambdaQuery(LendReturn.class)
                .eq(LendReturn::getLendId, lendId)
                .orderByAsc(LendReturn::getCurrentPeriod));
        return R.ok().data("items",lendReturns);
    }
}

